package com.redispro.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.BooleanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.redispro.dto.*;
import com.redispro.entity.*;
import com.redispro.mapper.FollowMapper;
import com.redispro.service.*;
import com.redispro.utils.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 李力
 * @since 2022-12-22
 */
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private IUserService userService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result follow(Long isFollowId, Boolean isFollow) {
        if (isFollowId == null || isFollow == null) {
            return Result.fail("参数为null");
        }
        Long id = UserHolder.getUser().getId();
        //关注
        if (isFollow) {
            Follow follow = new Follow();
            follow.setUserId(id);
            follow.setFollowUserId(isFollowId);
            boolean save = save(follow);
            if (save) {
                redisTemplate.opsForSet().add("follows:" + id, isFollowId.toString());
            }
        } else {
            //取关
            QueryWrapper<Follow> followQueryWrapper = new QueryWrapper<>();
            followQueryWrapper.eq("user_id", id);
            followQueryWrapper.eq("follow_user_id", isFollowId);
            boolean remove = remove(followQueryWrapper);
            if (remove) {
                redisTemplate.opsForSet().remove("follows:" + id, isFollowId.toString());
            }
        }
        return Result.ok();
    }

    @Override
    public Result isFollow(Long isFollowId) {
        if (isFollowId == null) {
            return Result.fail("参数为null");
        }
        Long id = UserHolder.getUser().getId();
        Boolean result = redisTemplate.opsForSet().isMember("follows:" + id, isFollowId.toString());
        return Result.ok(BooleanUtil.isTrue(result));
    }

    @Override
    public Result commonFollow(Long targetId) {
        if (targetId == null) {
            return Result.fail("参数为null");
        }
        Long currentUserId = UserHolder.getUser().getId();
        Set<String> intersect = redisTemplate.opsForSet().intersect("follows:" + currentUserId,
                "follows:" + targetId);
        if (intersect == null || intersect.isEmpty()) {
            return Result.ok(null);
        }
        List<Long> usersIds = intersect.stream().map(Long::valueOf).collect(Collectors.toList());
        List<User> users = userService.listByIds(usersIds);
        List<UserDTO> collect = users.stream().map(user -> BeanUtil.copyProperties(user, UserDTO.class)).collect(Collectors.toList());
        return Result.ok(collect);
    }
}
